'/
' GUI Library
' Written by Anton Berlin, 2010.
' 
' =========================================================================
' 
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU Lesser General Public License as published
' by the Free Software Foundation; either version 3 of the License.
' 
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU Lesser General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'/

superstrict

import "GUI_Gadget.bmx"

type GUI_Checkbox extends GUI_Gadget

	'// -------------------------------------------------------------------------------------------------------------
	'// Checkbox states (for non-global checkboxes).
	'// -------------------------------------------------------------------------------------------------------------
	global STATE_IDLE:byte = 0;
	global STATE_PRESSED:byte = 1;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Update this gadget.
	'// -------------------------------------------------------------------------------------------------------------
	method update()
	
		if (m_global) ..
			return;
		
		if (not mouseHovering()) ..
			m_state = STATE_IDLE;
		
		if (m_state = STATE_PRESSED) then
		
			if (not mouseDown(1)) then
			
				m_state = STATE_IDLE;
				check();
			
			end if
		
		end if
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Draw this gadget.
	'// -------------------------------------------------------------------------------------------------------------
	method draw()
	
		if (not m_visible) ..
			return;
		
		'// Setup the default drawing settings.
		drawutil.setColorInit(m_primitiveR, m_primitiveG, m_primitiveB);
		drawutil.setAlphaInit(m_transparency);
		drawutil.setFontInit(null);
		
		'// Draw the primitive of the gadget.
		if (m_global) ..
			drawOval(m_posX, m_posY, m_width, m_height); ..
		else ..
			drawRect(m_posX, m_posY, m_width, m_height);
		
		'// Draw the design of the gadget.
		design();
		
		'// Reset drawing settings.
		drawutil.resetColor();
		drawutil.resetAlpha();
		drawutil.resetFont();
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Draw the design of this gadget.
	'// -------------------------------------------------------------------------------------------------------------
	method design()
	
		'// Border.
		local color1R:int = m_primitiveR * 0.5;
		local color1G:int = m_primitiveG * 0.5;
		local color1B:int = m_primitiveB * 0.5;
		
		local color2R:int = m_primitiveR * 1.5;
		local color2G:int = m_primitiveG * 1.5;
		local color2B:int = m_primitiveB * 1.5;
		
		if (m_global) then
		
			setColor(color1R, color1G, color1B);
			drawOval(m_posX, m_posY, m_width - 1, m_height - 1);
			setColor(color2R, color2G, color2B);
			drawOval(m_posX + 1, m_posY + 1, m_width - 1, m_height - 1);
			setColor(m_primitiveR, m_primitiveG, m_primitiveB);
			drawOval(m_posX + 1, m_posY + 1, m_width - 2, m_height - 2);
		
		else
		
			setColor(color1R, color1G, color1B);
			drawLine(m_posX, m_posY, m_posX + m_width, m_posY, false);
			drawLine(m_posX, m_posY, m_posX, m_posY + m_height, false);
			setColor(color2R, color2G, color2B);
			drawLine(m_posX, m_posY + m_height, m_posX + m_width, m_posY + m_height, false);
			drawLine(m_posX + m_width, m_posY, m_posX + m_width, m_posY + m_height, false);
		
		end if
		
		if (not m_selected) ..
			return;
		
		'// ------------ Checkmark -----------
		
		local x:double = m_posX + m_width * 0.25;
		local y:double = m_posY + m_height * 0.25;
		
		if (m_global) then
		
			'// Shadow.
			if (m_useShadows) then
			
				setAlpha(m_transparency * 0.5);
				setColor(0, 0, 0);
				drawOval(x + 3, y + 3, m_width * 0.7, m_height * 0.7);
			
			end if
			
			if (m_useBorders) then
			
				'// Upper border.
				setAlpha(m_transparency * 1.0);
				setColor(m_markColorR * 0.1, m_markColorG * 0.1, m_markColorB * 0.1)
				drawOval(x, y, m_width * 0.5 - 1, m_height * 0.5 - 1);
				setAlpha(m_transparency * 1.0);
				setColor(m_markColorR * 0.1, m_markColorG * 0.1, m_markColorB * 0.1)
				drawOval(x, y + 1, m_width * 0.5 - 1, m_height * 0.5 - 1);
				setColor(m_markColorR * 0.1, m_markColorG * 0.1, m_markColorB * 0.1)
				drawOval(x + 1, y, m_width * 0.5 - 1, m_height * 0.5 - 1);
				
				'// Lower border.
				setColor(m_markColorR * 0.1, m_markColorG * 0.1, m_markColorB * 0.1)
				drawOval(x + 2, y + 2, m_width * 0.5 - 1, m_height * 0.5 - 1);
			
			end if
			
			'// Main mark.
			setColor(m_markColorR, m_markColorG, m_markColorB);
			drawOval(x + 1, y + 1, m_width * 0.5 - 1, m_height * 0.5 - 1);
		
		else
		
			'// Main mark.
			setColor(m_markColorR, m_markColorG, m_markColorB);
			drawLine(x - 1, y + m_height * 0.25, x + m_width * 0.25 - 1, y + m_height * 0.5);
			drawLine(x,     y + m_height * 0.25, x + m_width * 0.25,     y + m_height * 0.5);
			drawLine(x + 1, y + m_height * 0.25, x + m_width * 0.25 + 1, y + m_height * 0.5);
			drawLine(x + m_width * 0.25 - 1, y + m_height * 0.5, x + m_width * 0.5 - 1, y);
			drawLine(x + m_width * 0.25,     y + m_height * 0.5, x + m_width * 0.5,     y);
			drawLine(x + m_width * 0.25 + 1, y + m_height * 0.5, x + m_width * 0.5 + 1, y);
		
		end if
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Check this checkbox, if it's non-global.
	'// -------------------------------------------------------------------------------------------------------------
	method check()
	
		if (m_global) ..
			return;
		
		if (m_selected) ..
			m_selected = false; ..
		else ..
			m_selected = true;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Press the checkbox.
	'// -------------------------------------------------------------------------------------------------------------
	method press()
	
		if (mouseDown(1)) ..
			m_state = STATE_PRESSED;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set the color of the checkmark.
	'// -------------------------------------------------------------------------------------------------------------
	method setMarkColor(r:byte, g:byte, b:byte)
	
		m_markColorR = r;
		m_markColorG = g;
		m_markColorB = b;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set whether or not to use shadows on the marker.
	'// -------------------------------------------------------------------------------------------------------------
	method setMarkShadowUsage(value:byte)
	
		m_useShadows = value;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set whether or not to use borders on the marker.
	'// -------------------------------------------------------------------------------------------------------------
	method setMarkBorderUsage(value:byte)
	
		m_useBorders = value;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set whether or not to make this checkbox global.
	'// -------------------------------------------------------------------------------------------------------------
	method setGlobal(value:byte)
	
		m_global = value;
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Mark text color.
	'// -------------------------------------------------------------------------------------------------------------
	field m_markColorR:byte;
	field m_markColorG:byte;
	field m_markColorB:byte;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Flag that determines whether or not to use shadows on the marker.
	'// -------------------------------------------------------------------------------------------------------------
	field m_useShadows:byte = false;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Flag that determines whether or not to use borders on the marker.
	'// -------------------------------------------------------------------------------------------------------------
	field m_useBorders:byte = false;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// True if this checkbox is currently selected.
	'// -------------------------------------------------------------------------------------------------------------
	field m_selected:byte = false;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// If this is set to false, then the checkbox will be checked with a checkmark, and not a circle. The thing
	'// about non-global checkboxes is that you can check as many non-global checkboxes as you want. With global
	'// ones, you can only check one. Anywhere.
	'// -------------------------------------------------------------------------------------------------------------
	field m_global:byte = true;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Current checkbox state (for non-global checkboxes).
	'// -------------------------------------------------------------------------------------------------------------
	field m_state:byte = STATE_IDLE;

end type

